
// This class for resize the control
//
function ResizeHandler(config, controlPosX, controlPosY, controlWidth, controlHeight){
	
	// Properties
	//
	var props = {
		minWidth:10,
		minHeight:10,
		minLeft:0,
		maxLeft:9999,
		minTop:0,
		maxTop:9999,

		controlId: null,
		
	};
	
	for(var p in props)
		this[p] = (typeof config[p] == 'undefined') ? props[p] : config[p];
	
	// Fields
	//
	
	// Create the 8 anchor
	var border = '2px solid blue';
	var color = 'green';
	var selectedColor = 'red';
	var width = 17;
	var height = 17;
	
	this.anchors = new Array(
		addAnchor('tl', this.controlId,this),
		addAnchor('tm', this.controlId,this),
		addAnchor('tr', this.controlId,this),
		addAnchor('ml', this.controlId,this),
		addAnchor('mr', this.controlId,this),
		addAnchor('bl', this.controlId,this),
		addAnchor('bm', this.controlId,this),
		addAnchor('br', this.controlId,this)
	);
	
	// Public Method
	//
	
	// Function for select the control
	this.select = function(){
		
		//elm.style.zIndex = ++zIndex;
		
		for(var i = 0; i < this.anchors.length; i++) {
			var anchor = this.anchors[i];
			anchor.setVisible();
			
		}
		
		this.updateAnchor();
	};
	
	// Fucntion for deselect the control
	this.deselect = function(delHandles){
		for(var i = 0; i < this.anchors.length; i++) {
			var anchor = this.anchors[i];
			anchor.setInvisible();
			
		}
	};
	
	// Function for update the position, the size of the control and its Anchor
	this.update = function(offsetX, offsetY, type) {
		var control = document.getElementById(this.controlId);
		var offsetTop = control.offsetTop;
		var offsetLeft = control.offsetLeft;
		var currentWidth = parseInt(control.style.width);
		var currentHeight = parseInt(control.style.height);
		
		// Update position and size of control 
		//
		if(type.indexOf('t') >= 0){
			// Top
			control.style.top = (offsetTop + offsetY) + 'px';
			control.style.height = (currentHeight - offsetY) + 'px';
		}
		
		if(type.indexOf('l') >= 0) {
			// Left
			control.style.left = (offsetLeft + offsetX) + 'px';
			control.style.width = (currentWidth - offsetX) + 'px';
		}
		
		if(type.indexOf('r') >= 0) {
			// Right
			control.style.width = (currentWidth + offsetX) + 'px';
		}
				
		if(type.indexOf('b') >= 0) {
			// Bottom
			control.style.height = (currentHeight + offsetY) + 'px';
		}
		
		// Update the position of anchor
		//
		this.updateAnchor();
	}		

	// Update the position of the 8 anchors
	this.updateAnchor = function() {
		for(var i = 0; i < this.anchors.length; i++) {
			// Get anchor
			var anchor = this.anchors[i];
			
			// Call the update position method
			anchor.updatePosition(anchor.getType(), this.controlId, anchor.getAnchor());
			
		}
	}
	
	
	// Private Method
	//
	function addAnchor(type, controlId,controller) {
		return new Anchor(type, controlId, controlPosX, controlPosY, controlWidth, controlHeight, width, height, controller, border, color, selectedColor);
	}
}